package edu.nyu.cyrus;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class CyrusParser {

	public ArrayList<PersonDataModel> parseFile(String path, String delimiter)
			throws Exception {

		BufferedReader br = new BufferedReader(new FileReader(path));

		ArrayList<PersonDataModel> parsedData = new ArrayList<PersonDataModel>();

		try {
			String line;
			while ((line = br.readLine()) != null) {
				String[] splitLine = line.split(delimiter);
				for (int i =0; i< splitLine.length;i++) {
					splitLine[i] = splitLine[i].trim();
				}
				PersonDataModel data;
//					String color, String dob, String fname, String gender, String lname
				if (delimiter.equals("\\|")) {
					splitLine[5] = splitLine[5].replace('-', '/');
					splitLine[3] = splitLine[3].equals("M")? "Male" : "Female";
					data = new PersonDataModel(splitLine[4], splitLine[5],
							splitLine[1], splitLine[3], splitLine[0]);
				} else if (delimiter.equals(" ")) {
					splitLine[4] = splitLine[4].replace('-', '/');
					splitLine[3] = splitLine[3].equals("M")? "Male" : "Female";
					data = new PersonDataModel(splitLine[5], splitLine[4],
							splitLine[1], splitLine[3], splitLine[0]);
				} else if (delimiter.equals(",")) {
					splitLine[4].replace('-', '/');
					data = new PersonDataModel(splitLine[3], splitLine[4],
							splitLine[1], splitLine[2], splitLine[0]);

				} else {
					throw new Exception("Irregularity in data");
				}
				parsedData.add(data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}

		return parsedData;
	}
}
